package com.scorpio.system.service.impl;

import com.scorpio.common.config.ScorpioConfig;
import com.scorpio.common.config.ServerConfig;
import com.scorpio.common.constant.Constants;
import com.scorpio.common.exception.CustomException;
import com.scorpio.common.service.impl.BaseServiceImpl;
import com.scorpio.common.utils.file.FileUploadUtils;
import com.scorpio.redis.RedisCache;
import com.scorpio.redis.constant.RedisConstants;
import com.scorpio.system.domain.SysOss;
import com.scorpio.system.mapper.SysOssMapper;
import com.scorpio.system.service.ISysConfigService;
import com.scorpio.system.service.ISysOssService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

@Service
public class SysOssServiceImpl extends BaseServiceImpl<SysOssMapper, SysOss> implements ISysOssService {

    @Autowired
    private ISysConfigService configService;

    @Autowired
    private RedisCache redisCache;

    @Override
    public SysOss uploadImage(MultipartFile file) {
        try {
            // 上传文件路径
            String filePath = ScorpioConfig.getUploadPath();
            // 上传并返回新文件名称
            String path = FileUploadUtils.upload(filePath, file);
            SysOss oss = new SysOss();
            oss.setPath(path);
            if (baseDao.insert(oss) == 0) {
                throw new CustomException("数据写入失败");
            }
            String host = configService.selectConfigByKey(Constants.SYS_CONFIG_SERVER_HOST_KEY);
            oss.setUrl(host + path);
            return oss;
        } catch (IOException e) {
            throw new CustomException("上传失败", e);
        }
    }

    @Override
    public int add(SysOss sysOss) {
        int count = baseDao.insert(sysOss);
        if (count > 0) {
            refresh(sysOss.getTableName(), sysOss.getTableId());
        }
        return count;
    }

    @Override
    public int update(SysOss sysOss) {
        int count = baseDao.updateById(sysOss);
        refresh(sysOss.getTableName(), sysOss.getTableId());
        return count;
    }

    @Override
    public int deleteByIds(String tableName, Long tableId, Long[] ids) {
        int count = baseDao.deleteBatchIds(Arrays.asList(ids));
        refresh(tableName, tableId);
        return count;
    }


    @Override
    public List<SysOss> selectList(String tableName, Long tableId) {
        List<SysOss> list = redisCache.getCacheList(getRedisKey(tableName, tableId));
        if (list == null || list.size() == 0) {
            String host = configService.selectConfigByKey(Constants.SYS_CONFIG_SERVER_HOST_KEY);
            list = baseDao.selectList(
                    getQueryWrapper().eq("table_id", tableId)
                            .eq("table_name", tableName)
                            .orderByAsc("sort"));
            if (list != null && list.size() > 0) {
                for (SysOss sysOss : list) {
                    sysOss.setUrl(host + sysOss.getPath());
                }
                redisCache.setCacheList(getRedisKey(tableName, tableId), list);
            }
        }
        return list;
    }

    @Override
    public SysOss selectLatest(String tableName, Long tableId) {
        List<SysOss> list = selectList(tableName, tableId);
        if (list == null || list.size() == 0) {
            return list.get(list.size() - 1);
        }
        return new SysOss();
    }

    @Override
    public void refresh(String tableName, Long tableId) {
        redisCache.deleteObject(getRedisKey(tableName, tableId));
    }

    @Override
    public void refreshAll() {
        Collection<String> keys = redisCache.keys(RedisConstants.SYS_OSS_FILE_KET + "*");
        for (String key : keys) {
            redisCache.deleteObject(key);
        }
        redisCache.deleteObject(RedisConstants.SYS_OSS_FILE_KET + "*");
    }

    private String getRedisKey(String tableName, Long tableId) {
        return RedisConstants.SYS_OSS_FILE_KET + tableName + ":" + tableId;
    }

}
